/****************************** (C) COPYRIGHT 2016 Rsestudio ****************************
* File Name          : 
* Author             : RSES_Liyang
* Version            : V1.0.0
* Date               : 00-May-20
* Description        : 
*****************************************************************************************/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "Bsp.h"
#include "main.h"
#include "device_pins.h"
#include "PowerManager.h"
#include "oled.h" 	 
#include "oled_font.h"

//#define OLED_USE_SH1107		//OLD
#define OLED_USE_CH1115			//NEW

SemaphoreHandle_t oled_lock = NULL;     // 互斥量句柄
StaticSemaphore_t xoled_lock;  // 存储互斥量的数据结构

#define DISP_LOCK()					xSemaphoreTake(oled_lock,portMAX_DELAY);
#define DISP_UNLOCK()				xSemaphoreGive(oled_lock);

void oled_nop()
{
	uint16_t i;
	for(i=0;i<3;i++);
}

void OLED_send_Byte(uint8_t dat)
{	
	uint8_t i;			  
	for(i=0;i<8;i++)
	{			  
		OLED_SCL_Clr();
        oled_nop();
        
		if(dat&0x80)
        {
		   OLED_SDA_Set();
		}
		else
        {
		   OLED_SDA_Clr();
		}
        oled_nop();
		OLED_SCL_Set();
        oled_nop();
		dat<<=1;   
	}
    OLED_SCL_Clr();
    oled_nop();
}

void OLED_WR_Byte(uint8_t dat,uint8_t cmd)
{	
	uint8_t i;	
	
    if (cmd)    {OLED_DC_Set();}
    else        {OLED_DC_Clr();}
	  	
	OLED_CS_Clr();
	for (i=0;i<8;i++)
	{			  
		OLED_SCL_Clr();
		if(dat&0x80){
		   OLED_SDA_Set();
		}
		else {
		   OLED_SDA_Clr();
		}
		OLED_SCL_Set();
		dat<<=1;   
	}				 		  
	OLED_CS_Set();
	OLED_DC_Set();
}


//#if 0
//u8 OLED_GRAM[8][128];
//uint8_t bit_cov(uint8_t in)
//{
//	uint8_t tmp,i;
//	for(tmp=0,i=0;i<8;i++){
//		tmp<<=1;
//		(in & (1<<i))?(tmp|=0x01):(tmp&=0xfe);
//	}
//	return tmp;
//}

//void OLED_Refresh(void)
//{
//	uint8_t page,col;
//	for(page=0;page<8;page++){
//		OLED_WR_Byte(0xB0|(7-page),OLED_CMD);
//		OLED_WR_Byte(0x00,OLED_CMD);
//		OLED_WR_Byte(0x10,OLED_CMD);
//		for(col=0;col<128;col++){
//			OLED_WR_Byte(bit_cov(OLED_GRAM[page][col]),OLED_DATA);
//		}
//	}
//}
//void OLED_Clear(void)
//{
//	int i;
//	for(i=0;i<(8*128);i++)
//		((uint8_t*)OLED_GRAM)[i]=0;
//	OLED_Refresh();
//}
//void OLED_DrawPoint(uint8_t x,uint8_t y,uint8_t set)
//{	
//	uint8_t tmp,bit;
//	uint8_t *p;
//	bit = y%8;
//	p = &OLED_GRAM[y/8][x];
//	tmp = *p;
//	set?(tmp|=1<<bit):(tmp &=~(1<<bit));
//	*p=tmp;	
//}
//#else
#ifdef 	OLED_USE_SH1107
__align(4) u8 OLED_GRAM[16][64];
void OLED_Refresh(void)
{
	uint16_t page,col;
	for(page=0;page<16;page++)
	{
		OLED_WR_Byte(0xB0|page,OLED_CMD);
		OLED_WR_Byte(0x00,OLED_CMD);
		OLED_WR_Byte(0x12,OLED_CMD);  
		for(col=0;col<64;col++)
		{
			OLED_WR_Byte(OLED_GRAM[page][col],OLED_DATA);
		}
	}
}
void OLED_Clear(void)
{
	int i;
	for(i=0;i<(8*128);i++)
		((uint8_t*)OLED_GRAM)[i]=0;
}
void OLED_DrawPoint(uint8_t x,uint8_t y,uint8_t set)
{	
	uint8_t tmp,bit;
	uint8_t *p;
	bit = x%8;
	tmp = (x/8);
	if(tmp>15)
	{
		tmp = 15;
	}
	if(y>63)
	{
		y = 63;
	}
	p = &OLED_GRAM[x/8][y];
	tmp = *p;
	set?(tmp|=1<<bit):(tmp &=~(1<<bit));
	*p=tmp;	
}

void lcd_fillBitmap(u16 x,u16 y,u16 w,u16 h,u8 *Bitmap,u8 mode)
{	
	uint8_t i,j,k,l,tmp;
	for(i=0;i<h;i++)
	{
		for(j=0;j<w;j++)
		{
			k = i/8;
			l = i%8;
			tmp = Bitmap[k*w + j];
			OLED_DrawPoint(x + j,y + i,mode?(!(tmp&(1<<l))):((tmp&(1<<l))));
		}
	}

	
//    u16 xoff,yoff,i,bit;
//    u16 ByteCnt;
//	uint8_t temp;

//    ByteCnt=h/8;
//	
//    if(h%8)
//		ByteCnt+=1;
//	
//    ByteCnt*=w;

//	xoff=0;
//    for(i=0;i<ByteCnt;i++)
//	{
//		temp = Bitmap[i];
//		yoff=0;
//		for(bit=0;bit<8;bit++)
//		{
//			if(temp&0x01)
//				OLED_DrawPoint(x+xoff,y+yoff,!mode);
//			else 
//				OLED_DrawPoint(x+xoff,y+yoff,mode);
//			temp>>=1;
//			yoff++;
//		}
//		xoff++;
//		if(xoff>=w)
//		{
//			xoff=0;
//			y+=8;
//		}
//    }
}

#elif defined OLED_USE_CH1115
uint8_t OLED_GRAM[1040];

//void OLED_Refresh(void)
//{
//	uint8_t page,col;
//	for(page=0;page<8;page++)
//	{
//		OLED_WR_Byte(0xB0|page,OLED_CMD);
//		OLED_WR_Byte(0x00,OLED_CMD);
//		OLED_WR_Byte(0x10,OLED_CMD);  
//		for(col=0;col<128;col++)
//		{
//			OLED_WR_Byte(OLED_GRAM[page*128 + col],OLED_DATA);
//		}
//	}
//}



void OLED_Refresh(void)
{
	uint8_t page,col;
	uint8_t *p_data;
    
    //DISP_LOCK();
    
    OLED_WR_Byte(0x00,OLED_CMD); /*set lower column address*/ 
	OLED_WR_Byte(0x10,OLED_CMD); /*set higher column address*/
	
	OLED_WR_Byte(0xB0,OLED_CMD); /*set page address*/ 
	
	OLED_WR_Byte(0x40,OLED_CMD); /*set display start lines*/ 
	
	OLED_WR_Byte(0x81,OLED_CMD); /*contract control*/ 
	OLED_WR_Byte(0x64,OLED_CMD); /*4d*/ 
	
	OLED_WR_Byte(0x82,OLED_CMD); /* iref resistor set and adjust ISEG*/ 
	OLED_WR_Byte(0x00,OLED_CMD); 
	
	OLED_WR_Byte(0xA2,OLED_CMD); /*set seg pads hardware configuration*/ 
	OLED_WR_Byte(0xA4,OLED_CMD); /*Disable Entire Display On (0xA4/0xA5)*/ 
	OLED_WR_Byte(0xA6,OLED_CMD); /*normal / reverse*/ 
	OLED_WR_Byte(0xA8,OLED_CMD); /*multiplex ratio*/ 
	OLED_WR_Byte(0x7F,OLED_CMD); /*duty = 1/64*/ 
	
	OLED_WR_Byte(0xC0,OLED_CMD); /*Com scan direction 0XC0*/ 
	OLED_WR_Byte(0xA0,OLED_CMD); /*set segment remap 0xA0*/ 
	
	OLED_WR_Byte(0xD3,OLED_CMD); /*set display offset*/ 
	OLED_WR_Byte(0x00,OLED_CMD); /* */ 
	
	OLED_WR_Byte(0xD5,OLED_CMD); /*set osc division*/ 	 
	OLED_WR_Byte(0xD9,OLED_CMD); /*set pre-charge period*/ 
	OLED_WR_Byte(0x22,OLED_CMD); 
	OLED_WR_Byte(0xdb,OLED_CMD); /*set vcomh*/ 
	OLED_WR_Byte(0x40,OLED_CMD); 
	OLED_WR_Byte(0x31,OLED_CMD); /* Set pump 7.4v */ 
	OLED_WR_Byte(0xad,OLED_CMD); /*set charge pump enable*/ 
	OLED_WR_Byte(0x8b,OLED_CMD); /*Set DC-DC enable (0x8a=disable; 0x8b=enable) */ 
    
    
	for(page=0;page<8;page++)
	{
        OLED_DC_Clr();
		oled_nop();
        
        OLED_CS_Clr();
        oled_nop();
        		
		OLED_send_Byte(0x40);
        OLED_send_Byte(0x00);
		OLED_send_Byte(0x10); 
		OLED_send_Byte(0xB0|page);
				
        oled_nop();   
        OLED_CS_Set();
        
		OLED_DC_Set();
		oled_nop();
               
		OLED_CS_Clr();
        oled_nop();
        
		p_data = &OLED_GRAM[page*128];
		for(col=0;col<128;col++)
		{
			OLED_send_Byte(*p_data++);
		}
        oled_nop();
        OLED_CS_Set();
        oled_nop();
	}
    
	//DISP_UNLOCK();
}


void OLED_Clear(void)
{
	int i,j;
	for(i=0;i<8;i++)
	{
		for(j=0;j<128;j++)
		{		
			OLED_GRAM[i*128+j]=0;
		}
	}
}
void OLED_DrawPoint(uint8_t x,uint8_t y,uint8_t set)
{	
	uint8_t tmp,bit;
	uint8_t *p;
	uint16_t pos;
	bit = y%8;
	pos = (y/8)*128 + x;
	if(pos > 1024)
		pos = 1024;
	p = &OLED_GRAM[pos];
	tmp = *p;
	set?(tmp|=1<<bit):(tmp &=~(1<<bit));
	*p=tmp;	
}

void lcd_fillBitmap(uint16_t x,uint16_t y,uint16_t w,uint16_t h,uint8_t *Bitmap,uint8_t mode)
{
    uint16_t xoff,yoff,i,bit;
    uint16_t ByteCnt;
	uint8_t temp;

    ByteCnt=h/8;
    if(h%8)
		ByteCnt+=1;
    ByteCnt*=w;
	xoff=0;
    for(i=0;i<ByteCnt;i++)
	{
		temp = Bitmap[i];
		yoff=0;
		
		for(bit=0;bit<8;bit++)
		{
			if(temp&0x01)
				OLED_DrawPoint(x+xoff,y+yoff,!mode);
			else 
				OLED_DrawPoint(x+xoff,y+yoff,mode);
			
			temp>>=1;
			yoff++;
		}
		
		xoff++;
		if(xoff>=w)
		{
			xoff=0;
			y+=8;
		}
    }
}

#endif




void lcd_fillarea(uint16_t x,uint16_t y,uint16_t w,uint16_t h,uint8_t set)
{
		int i,j;
		for(i=0;i<w;i++){
			for(j=0;j<h;j++){
				OLED_DrawPoint(x+i,y+j,set);
			}
		}
}

void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t size1,uint8_t mode)
{
	uint8_t i,m,temp,size2,chr1;
	uint8_t x0=x,y0=y;
	if(size1==8)size2=6;
	else size2=(size1/8+((size1%8)?1:0))*(size1/2);
	chr1=chr-' ';
	for(i=0;i<size2;i++)
	{
		if(size1==8)
			  {temp=asc2_0806[chr1][i];}
		else if(size1==12)
        {temp=asc2_1206[chr1][i];}
		else if(size1==16)
        {temp=asc2_1608[chr1][i];}
		else if(size1==24)
        {temp=asc2_2412[chr1][i];}
		else return;
		for(m=0;m<8;m++)
		{
			if(temp&0x01)OLED_DrawPoint(x,y,!mode);
			else OLED_DrawPoint(x,y,mode);
			temp>>=1;
			y++;
		}
		x++;
		if((size1!=8)&&((x-x0)==size1/2))
		{x=x0;y0=y0+8;}
		y=y0;
  }
}

void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t size1,uint8_t mode)
{
	while((*chr>=' ')&&(*chr<='~'))
	{
		OLED_ShowChar(x,y,*chr,size1,mode);
		if(size1==8)x+=6;
		else x+=size1/2;
		chr++;
  }
}

uint32_t OLED_Pow(uint8_t m,uint8_t n)
{
	uint32_t result=1;
	while(n--)
	{
	  result*=m;
	}
	return result;
}

void OLED_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t size1,uint8_t mode)
{
	uint8_t t,temp,m=0;
    
	if(size1==8)
        m=2;
    
	for(t=0;t<len;t++)
	{
		temp=(num/OLED_Pow(10,len-t-1))%10;
        if(temp==0)
        {
          if(size1==24)
          {
            lcd_fillBitmap(x+(t*15),y,15,24,(uint8_t *)&big_num[temp],mode);
          }
          else
          {
            OLED_ShowChar(x+(size1/2+m)*t,y,'0',size1,mode);
          }
        }
        else 
        {
          if(size1==24)
          {
            lcd_fillBitmap(x+(t*15),y,15,24,(uint8_t *)&big_num[temp],mode);
          }
          else
          {
            OLED_ShowChar(x+(size1/2+m)*t,y,temp+'0',size1,mode);
          }
        }
    }
}



void lcd_disp_bat(uint8_t x,uint8_t y,uint8_t level,uint8_t charge_sts)
{
	uint8_t i,j,k;
	uint8_t c1,c2;
	
	for(i=0;i<20;i++)
	{
		c1 = icon_batt_frame[i];
		c2 = icon_batt_frame[i + 20];
		for(j=0;j<8;j++)
		{
			OLED_DrawPoint(x+i,y+j,c1&(1<<j));
		}
		for(j=0;j<3;j++)
		{
			OLED_DrawPoint(x+i,y+8+j,c2&(1<<j));
		}
	}
	
	
	if(charge_sts == 1)
	{
		if(iProfile.sys_status.Date.Second&0x01)
		{
			level = 0;
		}
	}
		
	k=2;
	for(i=0;i<14;i++)
	{
		if(level > k)
		{
			for(j=0;j<7;j++)
			{
				OLED_DrawPoint(x+17-i,y+2+j,1);
			}
		}
		else
		{
			for(j=0;j<7;j++)
			{
				OLED_DrawPoint(x+17-i,y+2+j,0);
			}
		}
		k = k+7;
	}
}


void lcd_disp_icon_bt(uint8_t x,uint8_t y,uint8_t sts)
{
	uint8_t i,j;
	uint8_t c1,c2;
	
	if(sts != 0)
	{
		for(i=0;i<9;i++)
		{
			c1 = icon_bt[i];
			c2 = icon_bt[i + 9];
			for(j=0;j<8;j++)
			{
				OLED_DrawPoint(x+i,y+j,c1&(1<<j));
			}
			for(j=0;j<5;j++)
			{
				OLED_DrawPoint(x+i,y+8+j,c2&(1<<j));
			}
		}
	}
	else
	{
		for(i=0;i<9;i++)
		{
			for(j=0;j<8;j++)
			{
				OLED_DrawPoint(x+i,y+j,0);
			}
			for(j=0;j<5;j++)
			{
				OLED_DrawPoint(x+i,y+8+j,0);
			}
		}
	}
}



void lcd_disp_icon_usb(uint8_t x,uint8_t y,uint8_t sts)
{
	uint8_t i,j;
	uint8_t c1,c2;
	
	if(sts != 0)
	{
		for(i=0;i<9;i++)
		{
			c1 = icon_usb[i];
			c2 = icon_usb[i + 9];
			for(j=0;j<8;j++)
			{
				OLED_DrawPoint(x+i,y+j,c1&(1<<j));
			}
			for(j=0;j<5;j++)
			{
				OLED_DrawPoint(x+i,y+8+j,c2&(1<<j));
			}
		}
	}
	else
	{
		for(i=0;i<9;i++)
		{
			for(j=0;j<8;j++)
			{
				OLED_DrawPoint(x+i,y+j,0);
			}
			for(j=0;j<5;j++)
			{
				OLED_DrawPoint(x+i,y+8+j,0);
			}
		}
	}
}

void lcd_disp_icon_wifi(uint8_t x,uint8_t y,uint8_t sts)
{
	uint8_t i,j;
	uint8_t c1,c2;
	// 1.从上到下
	// 2.从左先右
	if(sts != 0)
	{
		for(i=0;i<9;i++)   // x轴 +1 表示列   +1
		{
			c1 = icon_wifi[i];
			c2 = icon_wifi[i + 9];
			for(j=0;j<8;j++)
			{
				OLED_DrawPoint(x+i,y+j,c1&(1<<j));  // y周 +1 表示横+1
			}
			for(j=0;j<5;j++)
			{
				OLED_DrawPoint(x+i,y+8+j,c2&(1<<j));
			}
		}
	}
	else
	{
		for(i=0;i<9;i++)
		{
			for(j=0;j<8;j++)
			{
				OLED_DrawPoint(x+i,y+j,0);
			}
			for(j=0;j<5;j++)
			{
				OLED_DrawPoint(x+i,y+8+j,0);
			}
		}
	}
}

void lcd_disp_icon_nowifi(uint8_t x,uint8_t y,uint8_t sts)
{
	uint8_t i,j;
	uint8_t c1,c2;
	// 1.从上到下
	// 2.从左先右
	if(sts != 0)
	{
		for(i=0;i<9;i++)   // x轴 +1 表示列   +1
		{
			c1 = icon_nowifi[i];
			c2 = icon_nowifi[i + 9];
			for(j=0;j<8;j++)
			{
				OLED_DrawPoint(x+i,y+j,c1&(1<<j));  // y周 +1 表示横+1
			}
			for(j=0;j<5;j++)
			{
				OLED_DrawPoint(x+i,y+8+j,c2&(1<<j));
			}
		}
	}
	else
	{
		for(i=0;i<9;i++)
		{
			for(j=0;j<8;j++)
			{
				OLED_DrawPoint(x+i,y+j,0);
			}
			for(j=0;j<5;j++)
			{
				OLED_DrawPoint(x+i,y+8+j,0);
			}
		}
	}
}

void lcd_disp_icon_sample(uint8_t x,uint8_t y,uint8_t sample)
{
	uint8_t i,j;
	uint8_t c1,c2,c3,c4;
	uint8_t point;
    
   
    for(i=0;i<9;i++)
	{
        c1 = icon_sample[i];
        c2 = icon_sample[i + 9];
        for(j=0;j<8;j++)
        {
            OLED_DrawPoint(x+i,y+j,c1&(1<<j));
        }
        for(j=0;j<5;j++)
        {
            OLED_DrawPoint(x+i,y+8+j,c2&(1<<j));
        }
    }
    
    if(sample<99)
    {
        point = 1;
        
    }
    else
    {
        sample = sample/10;
        point = 0;
    }
 
    c1 = sample/10%10;
    c2 = sample%10;
    
    for(i=0;i<3;i++)    
    {
        c3 = ~Font_Num3x5[c1][i];
        c4 = ~Font_Num3x5[c2][i];
        for(j=0;j<5;j++)
        {
            OLED_DrawPoint(x+i+1,y+j + 5,c3&(1<<j));
            OLED_DrawPoint(x+i+5,y+j + 5,c4&(1<<j));
        }
    }
    
    if(point != 0)
    {
        OLED_DrawPoint(x+4,y+11,0);
    }
}



void OLED_Disp_Cond_Time(uint16_t Target,uint16_t Current)
{
    uint8_t i,j,k;
    uint8_t Dat_Bit[4];
    uint8_t TempData;
    
    Dat_Bit[0] = Current/100%10;
    Dat_Bit[1] = Current/10%10;
    Dat_Bit[2] = Current%10;
    
    for(i=0;i<3;i++)
    {
        for(k=0;k<10;k++)
        {
            TempData = Font_Num10x14[Dat_Bit[i]][k];
            for(j=0;j<8;j++)
            {
                OLED_DrawPoint(32+6+k+i*12,16+0+j,TempData&(1<<j));
            }
            
            TempData = Font_Num10x14[Dat_Bit[i]][k+10];
            for(j=0;j<6;j++)
            {
                OLED_DrawPoint(32+6+k+i*12,16+0+j+8,TempData&(1<<j));
            }
        }
    } 
    
    for(i=0;i<41;i++)
    {
        if(i%2)
            OLED_DrawPoint(32+2+i,16+15,1);
        else
            OLED_DrawPoint(32+2+i,16+15,0);
        if(i%2)
            OLED_DrawPoint(32+2+i,16+16,0); 
        else
            OLED_DrawPoint(32+2+i,16+16,1); 
    }
    
    Dat_Bit[0] = Target/100%10;
    Dat_Bit[1] = Target/10%10;
    Dat_Bit[2] = Target%10;
      
    
    for(i=0;i<3;i++)
    {
        for(k=0;k<10;k++)
        {
            TempData = Font_Num10x14[Dat_Bit[i]][k];
            for(j=0;j<8;j++)
            {
                OLED_DrawPoint(32+6+k+i*12,16+18+j,TempData&(1<<j));
            }
            
            TempData = Font_Num10x14[Dat_Bit[i]][k+10];
            for(j=0;j<6;j++)
            {
                OLED_DrawPoint(32+6+k+i*12,16+18+j+8,TempData&(1<<j));
            }
        }
    }  
    
    for(k=0;k<13;k++)
    {
        TempData = Pic_Second[k];
        for(j=0;j<8;j++)
        {
            OLED_DrawPoint(32+47+k,16+6+j,TempData&(1<<j));
        }
        
        TempData = Pic_Second[k+13];
        for(j=0;j<8;j++)
        {
            OLED_DrawPoint(32+47+k,16+6+j+8,TempData&(1<<j));
        }
        
        TempData = Pic_Second[k+26];
        for(j=0;j<4;j++)
        {
            OLED_DrawPoint(32+47+k,16+6+j+16,TempData&(1<<j));
        }
    } 
}


void OLED_Disp_Cond_Temperature(uint16_t Target,uint16_t Current)
{
    uint8_t i,j,k;
    uint8_t Dat_Bit[4];
    uint8_t TempData;
    
    Dat_Bit[0] = Current/100%10;
    Dat_Bit[1] = Current/10%10;
    Dat_Bit[2] = Current%10;
    
    for(i=0;i<3;i++)
    {
        for(k=0;k<10;k++)
        {
            TempData = Font_Num10x14[Dat_Bit[i]][k];
            for(j=0;j<8;j++)
            {
                OLED_DrawPoint(32+4+k+i*12,16+0+j,TempData&(1<<j));
            }
            
            TempData = Font_Num10x14[Dat_Bit[i]][k+10];
            for(j=0;j<6;j++)
            {
                OLED_DrawPoint(32+4+k+i*12,16+0+j+8,TempData&(1<<j));
            }
        }
    } 
    
    for(i=0;i<41;i++)
    {
        if(i%2)
            OLED_DrawPoint(32+i,16+15,1);
        else
            OLED_DrawPoint(32+i,16+15,0);
        if(i%2)
            OLED_DrawPoint(32+i,16+16,0); 
        else
            OLED_DrawPoint(32+i,16+16,1); 
    }
    
    Dat_Bit[0] = Target/100%10;
    Dat_Bit[1] = Target/10%10;
    Dat_Bit[2] = Target%10;
      
    
    for(i=0;i<3;i++)
    {
        for(k=0;k<10;k++)
        {
            TempData = Font_Num10x14[Dat_Bit[i]][k];
            for(j=0;j<8;j++)
            {
                OLED_DrawPoint(32+4+k+i*12,16+18+j,TempData&(1<<j));
            }
            
            TempData = Font_Num10x14[Dat_Bit[i]][k+10];
            for(j=0;j<6;j++)
            {
                OLED_DrawPoint(32+4+k+i*12,16+18+j+8,TempData&(1<<j));
            }
        }
    }  
    
    for(k=0;k<18;k++)
    {
        TempData = Pic_CDegree[k];
        for(j=0;j<8;j++)
        {
            OLED_DrawPoint(32+44+k,16+4+j,TempData&(1<<j));
        }
        
        TempData = Pic_CDegree[k+18];
        for(j=0;j<8;j++)
        {
            OLED_DrawPoint(32+44+k,16+4+j+8,TempData&(1<<j));
        }
        
        TempData = Pic_CDegree[k+36];
        for(j=0;j<4;j++)
        {
            OLED_DrawPoint(32+44+k,16+4+j+16,TempData&(1<<j));
        }
    } 
}

void refresh_ui_head(uint8_t init)
{
	static uint8_t sec_prev;
	uint16_t temp;
	uint8_t sec;
	uint8_t c;

	lcd_disp_bat(106,3,iProfile.sys_status.BatLevelStatus,iProfile.sys_status.BatChargeStatus);
    	
	if(init || sec_prev!=iProfile.sys_status.Date.Second)
	{
        
        if(iProfile.Ble.Status >=2)
        {
            lcd_disp_icon_bt(52,2,1);
        }
        else
        {
            lcd_disp_icon_bt(52,2,0);
        }
        
        if(iProfile.sys_status.HostConnect != 0)
        {
            lcd_disp_icon_usb(40,2,1);
        }
        else
        {
            lcd_disp_icon_usb(40,2,0);
        }
        
#if WIFI_ENABLE == 1
        if(WIFI_Connetstatusflag != 0)
            lcd_disp_icon_wifi(52,2,1);
        else
            lcd_disp_icon_wifi(52,2,0);
#endif 
        
        lcd_disp_icon_sample(64,2,iProfile.meas_params.SamplingSpan);
    
        
		sec_prev = iProfile.sys_status.Date.Second;
		sec = iProfile.sys_status.Date.Second;
  
		//lcd_fillarea(0,17,128,1,1); 
		
		for(temp=0;temp<128;temp++)
		{
			c = icon_line[temp];
			OLED_DrawPoint(temp,16,c&(1<<0));
			OLED_DrawPoint(temp,17,c&(1<<1));
			OLED_DrawPoint(temp,18,c&(1<<2));
			OLED_DrawPoint(temp,19,c&(1<<3));
		}
			
		OLED_ShowNum(2,0,iProfile.sys_status.Date.Hour,2,16,0);
		OLED_ShowNum(22,0,iProfile.sys_status.Date.Minute,2,16,0);
  
		if(sec&0x01)
		{
			OLED_DrawPoint(20,5,1);
			OLED_DrawPoint(19,5,1);
			OLED_DrawPoint(20,6,1);
			OLED_DrawPoint(19,6,1);
			
			OLED_DrawPoint(20,11,1);
			OLED_DrawPoint(19,11,1);
			OLED_DrawPoint(20,12,1);
			OLED_DrawPoint(19,12,1);
		}
		else
		{
			OLED_DrawPoint(20,5,0);
			OLED_DrawPoint(19,5,0);
			OLED_DrawPoint(20,6,0);
			OLED_DrawPoint(19,6,0);
			
			OLED_DrawPoint(20,11,0);
			OLED_DrawPoint(19,11,0);
			OLED_DrawPoint(20,12,0);
			OLED_DrawPoint(19,12,0);
		}
		
		temp = iProfile.sys_status.BatTempetature;
		OLED_ShowNum(75,0,temp/10,2,16,0);
		
		OLED_DrawPoint(92,12,1);
		OLED_DrawPoint(93,12,1);
		OLED_DrawPoint(92,13,1);
		OLED_DrawPoint(93,13,1);
		
		OLED_ShowNum(95,0,temp%10,1,16,0);
	}
}

void oled_off(void)
{
    OLED_WR_Byte(0xAE,OLED_CMD); /*display off*/ 
}

void oled_config(void)
{
    vTaskDelay(pdMS_TO_TICKS(10));  //rt_thread_delay(10);
    OLED_RES_Clr();
    vTaskDelay(pdMS_TO_TICKS(10));  //rt_thread_delay(10);
    OLED_RES_Set();
    vTaskDelay(pdMS_TO_TICKS(10));  //rt_thread_delay(10);
    
    OLED_WR_Byte(0xAE,OLED_CMD); /*display off*/ 
	
	OLED_WR_Byte(0x00,OLED_CMD); /*set lower column address*/ 
	OLED_WR_Byte(0x10,OLED_CMD); /*set higher column address*/
	
	OLED_WR_Byte(0xB0,OLED_CMD); /*set page address*/ 
	
	OLED_WR_Byte(0x40,OLED_CMD); /*set display start lines*/ 
	
	OLED_WR_Byte(0x81,OLED_CMD); /*contract control*/ 
	OLED_WR_Byte(0x64,OLED_CMD); /*4d*/ 
	
	OLED_WR_Byte(0x82,OLED_CMD); /* iref resistor set and adjust ISEG*/ 
	OLED_WR_Byte(0x00,OLED_CMD); 
	
	OLED_WR_Byte(0xA2,OLED_CMD); /*set seg pads hardware configuration*/ 
	OLED_WR_Byte(0xA4,OLED_CMD); /*Disable Entire Display On (0xA4/0xA5)*/ 
	OLED_WR_Byte(0xA6,OLED_CMD); /*normal / reverse*/ 
	OLED_WR_Byte(0xA8,OLED_CMD); /*multiplex ratio*/ 
	OLED_WR_Byte(0x7F,OLED_CMD); /*duty = 1/64*/ 
	
	OLED_WR_Byte(0xC0,OLED_CMD); /*Com scan direction 0XC0*/ 
	OLED_WR_Byte(0xA0,OLED_CMD); /*set segment remap 0xA0*/ 
	
	OLED_WR_Byte(0xD3,OLED_CMD); /*set display offset*/ 
	OLED_WR_Byte(0x00,OLED_CMD); /* */ 
	
	OLED_WR_Byte(0xD5,OLED_CMD); /*set osc division*/ 	 
	OLED_WR_Byte(0xD9,OLED_CMD); /*set pre-charge period*/ 
	OLED_WR_Byte(0x22,OLED_CMD); 
	OLED_WR_Byte(0xdb,OLED_CMD); /*set vcomh*/ 
	OLED_WR_Byte(0x40,OLED_CMD); 
	OLED_WR_Byte(0x31,OLED_CMD); /* Set pump 7.4v */ 
	OLED_WR_Byte(0xad,OLED_CMD); /*set charge pump enable*/ 
	OLED_WR_Byte(0x8b,OLED_CMD); /*Set DC-DC enable (0x8a=disable; 0x8b=enable) */ 
    
    OLED_WR_Byte(0xAF,OLED_CMD);
}



extern const uint8_t map_Vcan[];
extern const uint8_t start_cancel[];
#include "Bsp.h"
void OLED_Init(void)
{	
    vTaskDelay(pdMS_TO_TICKS(10));  //rt_thread_delay(10);
    OLED_RES_Clr();
    vTaskDelay(pdMS_TO_TICKS(10));  //rt_thread_delay(10);
    OLED_RES_Set();
    vTaskDelay(pdMS_TO_TICKS(10));  //rt_thread_delay(10);
    
    oled_lock = xSemaphoreCreateMutexStatic(&xoled_lock);
	if (oled_lock == NULL)
	{
		while(1);
	}
	
#ifdef 	OLED_USE_SH1107
	
	OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
	
	OLED_WR_Byte(0x00,OLED_CMD); /*set lower column address*/ 
	OLED_WR_Byte(0x10,OLED_CMD); /*set higher column address*/ 
	
	OLED_WR_Byte(0xB0,OLED_CMD); /*set page address*/

	OLED_WR_Byte(0x81,OLED_CMD); /*contract control*/
	OLED_WR_Byte(0x2f,OLED_CMD); /*128*/
	OLED_WR_Byte(0x20,OLED_CMD); /* Set Memory addressing mode (0x20/0x21) */

	OLED_WR_Byte(0xA4,OLED_CMD); /*Disable Entire Display On (0xA4/0xA5)*/ 

	OLED_WR_Byte(0xA6,OLED_CMD); /*normal / reverse*/
	OLED_WR_Byte(0xA8,OLED_CMD); /*multiplex ratio*/
	OLED_WR_Byte(0x7F,OLED_CMD); /*duty = 1/64*/


	OLED_WR_Byte(0xDC,OLED_CMD); /*set display offset*/ 
	OLED_WR_Byte(0x00,OLED_CMD); 
	OLED_WR_Byte(0xC8,OLED_CMD);
	OLED_WR_Byte(0xA0,OLED_CMD);

	
	OLED_WR_Byte(0xD5,OLED_CMD); /*set osc division*/
	OLED_WR_Byte(0x50,OLED_CMD); 

	OLED_WR_Byte(0xD9,OLED_CMD); /*set pre-charge period*/
	OLED_WR_Byte(0x22,OLED_CMD); 

	OLED_WR_Byte(0xdb,OLED_CMD); /*set vcomh*/
	OLED_WR_Byte(0x35,OLED_CMD); 

	OLED_WR_Byte(0xad,OLED_CMD); /*set charge pump enable*/ 
	OLED_WR_Byte(0x8a,OLED_CMD); /*Set DC-DC enable (a=0:disable; a=1:enable) */

	
#elif defined OLED_USE_CH1115
	
	OLED_WR_Byte(0xAE,OLED_CMD); /*display off*/ 
	
	OLED_WR_Byte(0x00,OLED_CMD); /*set lower column address*/ 
	OLED_WR_Byte(0x10,OLED_CMD); /*set higher column address*/
	
	OLED_WR_Byte(0xB0,OLED_CMD); /*set page address*/ 
	
	OLED_WR_Byte(0x40,OLED_CMD); /*set display start lines*/ 
	
	OLED_WR_Byte(0x81,OLED_CMD); /*contract control*/ 
	OLED_WR_Byte(0x64,OLED_CMD); /*4d*/ 
	
	OLED_WR_Byte(0x82,OLED_CMD); /* iref resistor set and adjust ISEG*/ 
	OLED_WR_Byte(0x00,OLED_CMD); 
	
	OLED_WR_Byte(0xA2,OLED_CMD); /*set seg pads hardware configuration*/ 
	OLED_WR_Byte(0xA4,OLED_CMD); /*Disable Entire Display On (0xA4/0xA5)*/ 
	OLED_WR_Byte(0xA6,OLED_CMD); /*normal / reverse*/ 
	OLED_WR_Byte(0xA8,OLED_CMD); /*multiplex ratio*/ 
	OLED_WR_Byte(0x7F,OLED_CMD); /*duty = 1/64*/ 
	
	OLED_WR_Byte(0xC0,OLED_CMD); /*Com scan direction 0XC0*/ 
	OLED_WR_Byte(0xA0,OLED_CMD); /*set segment remap 0xA0*/ 
	
	OLED_WR_Byte(0xD3,OLED_CMD); /*set display offset*/ 
	OLED_WR_Byte(0x00,OLED_CMD); /* */ 
	
	OLED_WR_Byte(0xD5,OLED_CMD); /*set osc division*/ 	 
	OLED_WR_Byte(0xD9,OLED_CMD); /*set pre-charge period*/ 
	OLED_WR_Byte(0x22,OLED_CMD); 
	OLED_WR_Byte(0xdb,OLED_CMD); /*set vcomh*/ 
	OLED_WR_Byte(0x40,OLED_CMD); 
	OLED_WR_Byte(0x31,OLED_CMD); /* Set pump 7.4v */ 
	OLED_WR_Byte(0xad,OLED_CMD); /*set charge pump enable*/ 
	OLED_WR_Byte(0x8b,OLED_CMD); /*Set DC-DC enable (0x8a=disable; 0x8b=enable) */ 
	
#endif
    
    OLED_Clear();
	OLED_Refresh();
	OLED_WR_Byte(0xAF,OLED_CMD);
}

